package com.yanxx.codegenerator.config;

import com.yanxx.codegenerator.CodeGenerator;
import com.yanxx.codegenerator.impl.PersistentCodeGenerator;
import com.yanxx.codegenerator.service.CodeGeneratorService;
import com.yanxx.codegenerator.storage.CodeGeneratorStorage;

import com.yanxx.codegenerator.storage.impl.FileStorage;
import com.yanxx.codegenerator.storage.impl.MemoryStorage;
import com.yanxx.codegenerator.storage.impl.RedisStorage;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;



/**
 * 编码生成器自动配置类
 */
@Configuration
@ConditionalOnClass(CodeGenerator.class)
@EnableConfigurationProperties(CodeGeneratorProperties.class)
public class CodeGeneratorAutoConfiguration {

    private final CodeGeneratorProperties properties;

    public CodeGeneratorAutoConfiguration(CodeGeneratorProperties properties) {
        this.properties = properties;
    }

    @Bean
    @ConditionalOnMissingBean
    public CodeGenerator codeGenerator() {
        String storageType = properties.getStorageType();
        CodeGeneratorStorage storage;

        switch (storageType.toLowerCase()) {
            case "file":
                storage = new FileStorage(properties.getFileStorage().getPath());
                break;
            case "redis":
                storage = new RedisStorage(jedisPool(), properties.getRedisStorage().getKeyPrefix());
                break;
            case "database":
                // 数据源由Spring Boot自动配置提供，这里不需要手动创建
                // 如果需要数据库存储，用户需要在配置中提供数据源配置
                storage = new MemoryStorage(); // 默认使用内存存储
                break;
            case "memory":
            default:
                storage = new MemoryStorage();
                break;
        }

        return new PersistentCodeGenerator(storage);
    }

    @Bean
    @ConditionalOnMissingBean
    public CodeGeneratorService codeGeneratorService(CodeGenerator codeGenerator) {
        return new CodeGeneratorService(codeGenerator);
    }

    @Bean
    @ConditionalOnMissingBean
    @ConditionalOnClass(JedisPool.class)
    public JedisPool jedisPool() {
        try {
            JedisPoolConfig config = new JedisPoolConfig();
            // 从Spring环境中获取Redis配置
            String host = System.getProperty("spring.redis.host", "localhost");
            int port = Integer.parseInt(System.getProperty("spring.redis.port", "6379"));
            String password = System.getProperty("spring.redis.password", "");
            int database = Integer.parseInt(System.getProperty("spring.redis.database", "0"));

            if (password != null && !password.isEmpty()) {
                return new JedisPool(config, host, port, 2000, password, database);
            } else {
                return new JedisPool(config, host, port, 2000, null, database);
            }
        } catch (Exception e) {
            // 如果Redis配置有问题，返回null
            return null;
        }
    }


}